<!doctype html>
<html>

<head>
	<meta charset="utf-8">
	<title></title>
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	<link rel="stylesheet" type="text/css" href="../../../css/mui.min.css" />
	<link rel="stylesheet" type="text/css" href="../../../css/pages/issue.css" />
</head>

<body>
	<div class="content" id="app">
		<div class="name m-t-15">
			<span class="title m-b-10">车牌号</span>
			<div class="input p-b-10">
				<input type="text" v-model="addData.vehmNumberPlate" placeholder="请输入车牌号" />
			</div>
		</div>
		<div class="name">
			<span class="title m-b-10">车辆名称</span>
			<div class="input p-b-10">
				<input type="text" v-model="addData.vehmName" placeholder="请输入车辆名称" />
			</div>
		</div>
		<div class="name">
			<span class="title m-b-10">车辆颜色</span>
			<div class="input p-b-10">
				<input type="text" v-model="addData.vehmColour" placeholder="请输入车辆颜色" />
			</div>
		</div>
		<div class="p-30"></div>
		<div class="p-30"></div>
		<div class="p-30"></div>
		<div class="botton-wapper" @click="saveTap">保存</div>
	</div>
	<script src="../../../js/Width.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../../js/mui.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../../js/vue.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../../js/Util.js" type="text/javascript" charset="utf-8"></script>
	<script src="https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js"></script>
	<script type="text/javascript">
		mui.init()
		mui.plusReady(function () {
			if (!mui.os.android) {
				FastClick.attach(document.body);
			}
			var data = plus.webview.currentWebview().data;
			if(data){
			   console.log('存在')	
			   app.addData = data
			   app.isNew = false
			}
		})
		
		
		
		var app = new Vue({
			el: '#app',
			data: {
				isNew: true,
				addData: {
					vehmNumberPlate: '',
					vehmName: '',
					vehmColour: ''
				}
			},
			methods: {
				saveTap: function () {
					var addData = this.addData
					if (this.verification(addData)) {
						plus.nativeUI.showWaiting('提交中', {
							width: '100px',
							height: '100px'
						});
						if(this.isNew){
							this.PostVehicleManagement(addData)
						}else {
							console.log('修改')
							this.UpdateVehicleManagement(addData)
						}
					}
				},
				verification: function (data) { // 字符串遍历 判断是否都填写完整,
					var state;
					Object.keys(data).forEach(function (key) {
						console.log(key + ':' + data[key])
						if (!data[key]) {
							mui.toast("请填写完整")
							state = false
						} else {
							state = true
						}
					});
					return state;
				},
				PostVehicleManagement: function (data) { // 新增
					mui.post(Util.HTTP + 'api/VehicleManagement/PostVehicleManagement', {
						userID: Util.getState('$state').userID,
						vehmNumberPlate: data.vehmNumberPlate,
						vehmName: data.vehmName,
						vehmColour: data.vehmColour
					}, function (res) {
						plus.nativeUI.closeWaiting();
						if (res.code == 1001) {
							mui.toast('保存成功')
							setTimeout(function () {
								mui.back()
								var list = plus.webview.currentWebview().opener();
								//触发父页面的自定义事件(refresh),从而进行刷新
								mui.fire(list, 'issueBack');
								//返回true,继续页面关闭逻辑
								return true;
							}, 1000)
						}
					})
				},
				UpdateVehicleManagement:function(data) { // 修改
					mui.post(Util.HTTP+ 'api/VehicleManagement/UpdateVehicleManagement',{
						vehmID: data.vehmID,
						vehmNumberPlate: data.vehmNumberPlate,
						vehmName: data.vehmName,
						vehmColour: data.vehmColour
					},function(res){
						plus.nativeUI.closeWaiting();
						if (res.code == 1001) {
							mui.toast('保存成功')
							setTimeout(function () {
								mui.back()
								var list = plus.webview.currentWebview().opener();
								//触发父页面的自定义事件(refresh),从而进行刷新
								mui.fire(list, 'issueBack');
								//返回true,继续页面关闭逻辑
								return true;
							}, 1000)
						}
					})
				}
			}
		})
	</script>
</body>

</html>
